package com.shop.controller;


import com.shop.config.MessageConfig;
import com.shop.entity.Message;
import com.shop.entity.Order;
import com.shop.service.MessageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostMapping("merchantSend")
    public void merchantSend(@RequestBody Message message){
        rabbitTemplate.convertAndSend(MessageConfig.Merchant_Exchange,"merchant"+message.getMerchantId(),message.getData());
    }


    @PostMapping("orderMessage")
    public void orderMessage(@RequestBody Message message)
    {
        rabbitTemplate.convertAndSend(MessageConfig.TTL_EXCHANGE,"ttl.order",message.getData());
    }

    @RabbitListener(queues = {MessageConfig.DLX_QUEUE})
    public void updateOrderStatus( String id){
        System.out.println(id);
        messageService.updateOrderStatus(id);
    }


}
